<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<jsp:include page="../common/file.jsp"/>
<head>
	<!-- MenuTree -->
    <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/assets/css/menu_list.css">
    <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/assets/css/menu.css">
</head>
<body>
<script type="text/javascript">
	//初始化滚动条
	$("html").niceScroll({cursorcolor:"#000000",zindex:999999,bouncescroll:true,cursoropacitymax:0.4,cursorborder:"",cursorborderradius:7,cursorwidth:"7px",background:"rgba(0,0,0,.1)",autohidemode:true,railpadding:{top:0,right:2,left:2,bottom:0}});
	//页面信息提示
	var returnMsg = "${_error}";
	var authoriMsg = "${_authoriError}";
</script>
<div id="title-breadcrumb-option-demo" class="page-title-breadcrumb">
    <ol class="breadcrumb page-breadcrumb pull-left">
        <li><i class="fa fa-home"></i>&nbsp;<a href="${pageContext.request.contextPath}/organ/list">系统管理</a>&nbsp;&nbsp;<i class="fa fa-angle-right"></i>&nbsp;</li>
        <li class="active">单位管理</li>
    </ol>
</div>
<div style="clear:both"></div>
<div class="main-right-content">
	<div class="main-right-div">
		<div class="main-right-query-div">
			<form id="query-form-1" action="${pageContext.request.contextPath}/organ/list" method="post">
			<div class="main-right-search row">
				<c:if test="${sessionScope.usertype eq '1'}">
					<div class="col-lg-2">
						单位名称:<input type="text" id="organName" name="organName" maxlength="18" value="<c:out value='${organ.organName}'/>" placeholder="请输入机构名称" />
					</div>
				</c:if>
				<div class="col-lg-2">
					<c:if test="${sessionScope.usertype eq '1'}">
						&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						<button type="button" class="btn btn-blue btn-query">查 询</button>
						<button type="button" class="btn btn-gray btn-rest">重 置</button>
	                	<button type="button" class="btn btn-red btn-add">新 增</button>
                	</c:if>
                </div>
			</div>
			</form>
		</div>
		<div style="clear: both;"></div>
		<div class="main-right-table-div">
			<table class="main-right-table">
				<colgroup>
					<col style="width:5%;">
					<col style="width:8%;">
					<col style="width:3%;">
					<col style="width:6%;">
				</colgroup>
				<thead>
					<tr>
						<td>单位编号</td>
						<td>单位名称</td>
						<td>状态</td>
						<td>操作</td>
					</tr>
				</thead>
				<tbody>
					<c:if test="${list != null && list.size() > 0}">
					<c:forEach items="${list}" var="d" varStatus="st">
						<tr class="${d.parentId}" id="${d.organId}">
							<td class="tree-organId">${d.organId}</td>
							<c:if test="${d.level == null}">
								<td style="text-align: left;" class="yijicaidan">
									<span class="tree-hit tree-expanded" value="${d.organId}"></span>
									<span class="tree-icon tree-folder icon-company tree-folder-open"></span>
									<span style="color:red;font-weight:bold;font-size:14px">${d.organName}</span>
								</td>
							</c:if>
							<c:if test="${d.level != null}">
								<td style="text-align: left;" class="erjicaidan">
									<span style="margin-left: ${d.level}px" class="tree-hit tree-expanded" value="${d.organId}"></span>
									<span class="tree-icon tree-folder tree-folder-open icon-folder"></span>
									<span>${d.organName}</span>
								</td>
							</c:if>
							<td style="text-align: center;">
								<c:if test="${d.status == '1'}">
									<span>正常</span>
								</c:if>
								<c:if test="${d.status == '2'}">
									<span>已删除</span>
								</c:if>
							</td>
							<td style="text-align: center;">
								<c:if test="${d.organId != '110'}">
									<a data-id="<c:out value='${d.organId}'/>" class="stylebuttonwee stylemedium stylesquare styleyellow row-query-btn" href="javascript:void(0);">详情</a>
									<c:if test="${sessionScope.usertype eq '1'}">
									&nbsp;
									<a data-id="<c:out value='${d.organId}'/>" class="stylebuttonwee stylemedium stylesquare stylegreen row-update-btn" href="javascript:void(0);">修改</a>
									&nbsp;
									<a data-id="<c:out value='${d.organId}'/>" class="stylebuttonwee stylemedium stylesquare stylegrey row-delete-btn" href="javascript:void(0);">删除</a>
									</c:if>
								</c:if>
							</td>
						</tr>
					</c:forEach>
					</c:if>
					<c:if test="${list == null || list.size() <= 0}">
						<tr>
							<td colspan="4" style="padding: 5px;text-align: center;">查询无记录！</td>
						</tr>
					</c:if>
				</tbody>
			</table>
		</div>
	</div>
</div>
	<script type="text/javascript">
		var ctxt = "${pageContext.request.contextPath}";
		
		// 查询
		$('.btn-query').click(function() {
			$('#query-form-1').submit();
		});
		
		// 重置
		$('.btn-rest').click(function() {
			window.location.href = $('#query-form-1').attr("action");
		});
		
		// 单位详情
		$('.row-query-btn').click(function() {
			var organId = $(this).data('id');
			art.dialog.open(ctxt + '/organ/query?organId=' + organId, {
				title: '单位详情',width: 380,height: 275,lock: true,opacity: 0.1,cancel:true
			});
		});
		
		// 新增单位
		$('.btn-add').click(function() {
			art.dialog.open(ctxt + '/organ/create', {
				title: '新增单位',width: 380,height: 370,lock: true,opacity: 0.3
			});
		});
		
		// 修改单位
		$('.row-update-btn').click(function() {
			var organId = $(this).data('id');
			art.dialog.open(ctxt + '/organ/edit?organId='+organId, {
				title: '修改单位',width: 380,height: 325,lock: true,opacity: 0.3
			});
		});
		
		// 删除单位
		$('.row-delete-btn').click(function() {
			var organId = $(this).data('id');
			art.dialog.confirm('你确定要删除该单位吗？', function () {
				art.dialog.tips('正在提交...', 0.5);
				$.post(ctxt + "/organ/delete", {"organId": organId}, function(msg) {
					if (msg != undefined && msg.status == 0) {
						art.dialog.alert(msg.message, function() {
							$('#query-form-1').submit();
						}); 
					} else {
						art.dialog.tips(msg.message, 2);
					}
				},'json');
			}, function () {
			    art.dialog.tips('执行已取消', 0.3);
			});
		});
		
		// 一级展开
		$('.yijicaidan .tree-hit').click(function() {
			var val=$(this).attr("class");
			var array=val.split(" ");
			var menuId = $(this).attr("value");
			if(array.length < 2) {
				return false;
			}
			if(array[1] == 'tree-collapsed') {// 展开
				// 展开子单位
				expandedChildren(menuId);
				$(this).removeClass("tree-collapsed");
				$(this).attr("class", "tree-hit tree-expanded");
			} else {// 伸缩
				// 伸缩子单位
				collapsedChildren(menuId);
				$(this).removeClass("tree-expanded");
				$(this).attr("class", "tree-hit tree-collapsed");
			}
		});
		
		// 二级展开
		$('.erjicaidan .tree-hit').click(function() {
			var val=$(this).attr("class");
			var array=val.split(" ");
			var menuId = $(this).attr("value");
			if(array.length < 2) {
				return false;
			}
			if(array[1] == 'tree-collapsed') {// 展开
				// 展开子单位
				expandedChildren(menuId);
				$(this).removeClass("tree-collapsed");
				$(this).attr("class", "tree-hit tree-expanded");
			} else {// 伸缩
				// 伸缩子单位
				collapsedChildren(menuId);
				$(this).removeClass("tree-expanded");
				$(this).attr("class", "tree-hit tree-collapsed");
			}
		});
		
		// 页面初始化单位判断
		//$(".yijicaidan span").removeClass("tree-collapsed");
		$(".yijicaidan span").removeClass("tree-expanded");
		$('tr').each(function(){
			//if($(this).children(".tree-parentid").text()!=null && $(this).children(".tree-parentid").text()!='' && $(this).children(".tree-level").text() != 0) {
			//	$(this).css("display","none");
			//}
			$(this).children("td").children(".tree-hit").removeClass("tree-expanded");
			var menuId = $(this).children(".tree-menuid").text();
			$('tr').each(function(){
				var parentId = $(this).children(".tree-parentid").text();
				if(menuId == parentId && parentId != null && parentId != '') {
					$("#"+parentId).children("td").children(".tree-hit").attr("class", "tree-hit tree-expanded");
				}
			});
		});
		
		// 展开子单位
		function expandedChildren(menuId) {
			$('tr').each(function(){
				var x = $(this).attr("class");
				var y = $(this).children(".erjicaidan").children(".tree-hit").attr("value");
				if(x == menuId) {
					$(this).css("display",'');
					//expandedChildren(y);
				}
			});
		}
		
		// 伸缩子单位
		function collapsedChildren(menuId) {
			$('tr').each(function(){
				if($(this).attr("class") == menuId) {
					if($(this).children(".erjicaidan").children(".tree-hit").attr("class").split(" ").length == 2) {
						$(this).children(".erjicaidan").children(".tree-hit").removeClass("tree-expanded");
						$(this).children(".erjicaidan").children(".tree-hit").attr("class", "tree-hit tree-collapsed");
					}
					$(this).css("display","none");
					var y = $(this).children(".tree-menuid").text();
					collapsedChildren(y);
				}
			});
		}
	</script>
</body>